
/****app 使用apicloud **/

function fs_save(key, value) {
    PageVersionCache = PageVersionCache || 1;
    var path = 'fs://page_cache/' + PageVersionCache + '/' + key + '.txt';
    if (key === null) {
        _delete_dir("fs://page_cache");
        return;
    }
    if (typeof(value) == "undefined" || value == "") {
        return;
    }
    if (typeof(value) == "string") {
        value = "str-" + value;
    }
    if (typeof(value) == "object") {
        value = "obj-" + JSON.stringify(value);
    }
    if (value === null) {
        _delete_file(path);
        return;
    }
    var fs = api.require('fs'); //'fs://floder'
    fs.getAttribute({
        path: path
    }, function(ret, err) {
        if (ret.status) {
            //_read_file(path);
            _write_file(path, value);
        } else {
            fs.createFile({
                path: path
            }, function(ret, err) {
                if (ret.status) {
                    _write_file(path, value);
                } else {
                    //alert( "创建缓存文件失败:"+JSON.stringify( err ) );
                }
            });
        }
    });
}

function fs_get(key, f1, f2) {
    PageVersionCache = PageVersionCache || 1;
    var path = 'fs://page_cache/' + PageVersionCache + '/' + key + '.txt';
    _read_file(path, f1, f2);
}

/*****删除文件夹****/

function _delete_dir(path) {
    var fs = api.require('fs'); //'fs://floder'
    fs.remove({
        path: path
    }, function(ret, err) {
        if (ret.status) {
            //alert( JSON.stringify( ret ) );
        } else {
            //alert( JSON.stringify( err ) );
        }
    });
}

/*****删除文件****/

function _delete_file(path) {
    var fs = api.require('fs');
    fs.remove({
        path: path
    }, function(ret, err) {
        if (ret.status) {
            //alert( JSON.stringify( ret ) );
        } else {
            //alert( JSON.stringify( err ) );
        }
    });
}
/*****写入文件****/

function _write_file(path, value) {
    var fs = api.require('fs');
    fs.open({
        path: path,
        flags: 'read_write'
    }, function(ret, err) {
        if (ret.status) {
            var fd_id = ret.fd;
            fs.write({
                fd: fd_id,
                data: value,
                offset: 0
            }, function(ret, err) {
                if (ret.status) {
                    //alert( "写入成功了："+JSON.stringify( ret ) );
                } else {
                    //alert( "写入页面缓存失败了:"+JSON.stringify( err ) );
                }
            });
            _colse_file(fd_id);
        } else {
            //alert( "打开失败了:"+JSON.stringify( err ) );
        }
    });
}


function _read_file(path, f1, f2) {
    var fs = api.require('fs');
    fs.open({
        path: path,
        flags: 'read'
    }, function(ret, err) {
        if (ret.status) {
            var fd_id = ret.fd;
            fs.read({
                fd: fd_id,
            }, function(ret, err) {
                if (ret.status) {
                    var data = ret.data;
                    if (data.indexOf('str-') === 0) {
                        data = data.slice(4);
                        //alert( "读成功了："+data );
                    }
                    if (data.indexOf('obj-') === 0) {
                        data = data.slice(4);
                        data = JSON.parse(data);
                        //alert( "读成功了："+JSON.stringify( data ) );
                    }
                    //alert( "页面使用了缓存");
                    f1(data);

                } else {
                    f2();
                    //alert( "读取缓存失败了:"+JSON.stringify( err ) );
                }
                _colse_file(fd_id);

            });
        } else {
            f2();
            //alert( "打开缓存失败了:"+JSON.stringify( err ) );
        }
    });
}


function _colse_file(fd_id) {
    var fs = api.require('fs');
    fs.close({
        fd: fd_id
    });
}